/* locktestProc2.c
 *	Simple program to test the lock and CV-related syscalls across processes. 
 *locktestProc1.c will create a lock and CV, this program will try to interact with them. 
 *It should not be allowed to.
 *This program will be exec'd from locktestProc1.c

 */
 
#include "syscall.h"

int Proc2Lock;
int Proc2CV;

int Proc1Lock;
int Proc1CV;


int main(int argc, char **argv)
{   
	/* Must run tests with -d t to see all error output\n");*/
	Proc2Lock = CreateLock();
	Proc2CV = CreateCV();
	if(Proc2Lock != 1)
	{
		PrintOut("\nProcess 2: Error running test!..............................\n", 66);
		return;
	}
	if(Proc2CV != 1)
	{
		PrintOut("\nProcess 2: Error running test!..............................\n", 66);
		return;
	}

	/* If reach this point, know that index for lock created in Process 1 = 0
	and index for CV created in Process 1 = 0
	so can set locals here to those values*/
	Proc1Lock = 0;
	Proc1CV = 0;

	/*Will try to interact with lock created by first process, which should be the first lock (index 0) in user lock array*/
	PrintOut("\nProcess 2: Trying to Acquire a lock owned by first process......\n", 66);
	Acquire(Proc1Lock,1); /*Lock created in other process is at index 0*/
	PrintOut("\nProcess 2: Trying to Release a lock owned by first process......\n", 66);
	Release(Proc1Lock,1); 
	PrintOut("\nProcess 2: Trying to Destroy a lock owned by first process......\n", 66);
	DestroyLock(Proc1Lock,1); 

	/*Will try to interact with lock created by first process,
	which should be the first lock (index 0) in user lock array,
	while using a CV created by this process*/
	PrintOut("\nProcess 2: Trying to Signal with lock created by first process..\n", 66);
	Signal(Proc2CV,Proc1Lock,1); /*Lock created in other process is at index 0*/
	PrintOut("\nProcess 2: Trying to Broadcast with lock created by first process\n", 67);
	Broadcast(Proc2CV,Proc1Lock,1); 
	PrintOut("\nProcess 2: Trying to Wait with lock created by first process....\n", 66);
	Wait(Proc2CV,Proc1Lock,1); 

	/*Will try to interact with CV created by first process, 
	which should be the first CV (index 0) in user CV array,
	while using a lock created by this process*/
	PrintOut("\nProcess 2: Trying to Signal a CV created by first process.......\n", 66);
	Signal(Proc1CV,Proc2Lock,1); /*Lock created in other process is at index 0*/
	PrintOut("\nProcess 2: Trying to Broadcast a CV created by first process....\n", 66);
	Broadcast(Proc1CV,Proc2Lock,1); 
	PrintOut("\nProcess 2: Trying to Wait a CV created by first process.........\n", 66);
	Wait(Proc1CV,Proc2Lock,1); 
	PrintOut("\nProcess 2: Trying to Destroy a CV created by first process......\n", 66);
	DestroyCV(Proc1CV,1); 
}

